package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecoilMacroGenerator {

    /**
     * 生成压枪鼠标移动点
     * @param totalTime 总体耗时（毫秒）
     * @param pixelLength 压枪y轴像素长度
     * @param startX 起始x坐标
     * @param startY 起始y坐标
     * @return 鼠标移动点列表
     */
    public static List<MousePoint> generateRecoilPoints(int totalTime, int pixelLength, int startX, int startY) {
        List<MousePoint> points = new ArrayList<>();
        Random random = new Random();

        // 添加起始点
        points.add(new MousePoint(startX, startY, 0));

        // 计算平均需要的点数（每10-20毫秒一个点）
        int avgPointCount = totalTime / 15;
        if (avgPointCount < 10) avgPointCount = 10; // 最少10个点
        if (avgPointCount > 100) avgPointCount = 100; // 最多100个点

        // 计算每个点的y坐标增量
        double yIncrement = (double) pixelLength / (avgPointCount - 1);

        int remainingTime = totalTime;
        double currentY = startY;

        // 生成中间点
        for (int i = 1; i < avgPointCount - 1; i++) {
            // 随机时间间隔（5-20毫秒，但确保总时间不超过）
            int maxTime = Math.min(20, remainingTime / (avgPointCount - i));
            int minTime = Math.max(5, maxTime / 3);

            int timeInterval;
            if (minTime >= maxTime) {
                timeInterval = minTime;
            } else {
                timeInterval = random.nextInt(maxTime - minTime) + minTime;
            }

            // 更新剩余时间
            remainingTime -= timeInterval;

            // 更新y坐标
            currentY += yIncrement;

            // 添加点
            points.add(new MousePoint(startX, (int) Math.round(currentY),
                    points.get(points.size() - 1).getTime() + timeInterval));
        }

        // 添加终点，确保总时间准确
        int lastTimeInterval = totalTime - points.get(points.size() - 1).getTime();
        points.add(new MousePoint(startX, startY + pixelLength, totalTime));

        return points;
    }

    /**
     * 生成XML格式的鼠标移动点
     * @param totalTime 总体耗时（毫秒）
     * @param pixelLength 压枪y轴像素长度
     * @param startX 起始x坐标
     * @param startY 起始y坐标
     * @return XML格式字符串
     */
    public static String generateRecoilXML(int totalTime, int pixelLength, int startX, int startY) {
        List<MousePoint> points = generateRecoilPoints(totalTime, pixelLength, startX, startY);

        StringBuilder xmlBuilder = new StringBuilder();
        MousePoint prev = new MousePoint(0, 0, 0);
        for (MousePoint point : points) {
            xmlBuilder.append("            <Buffer>\n")
                    .append("               <x>").append(point.getX()).append("</x>\n")
                    .append("               <y>").append(point.getY()).append("</y>\n")
                    .append("               <time>").append(point.getTime() - prev.getTime()).append("</time>\n")
                    .append("            </Buffer>\n");
            prev = point;
        }
        xmlBuilder.append("            <Buffer>\n")
                .append("               <x>").append(startX).append("</x>\n")
                .append("               <y>").append(startY).append("</y>\n")
                .append("               <time>").append(2).append("</time>\n")
                .append("            </Buffer>\n");
        return xmlBuilder.toString();
    }

    public static void main(String[] args) {
        // 示例：生成850ms，压枪90像素的宏
        String xml = generateRecoilXML(350, 70, 640, 360);
        System.out.println(xml);

        // 可以调整参数测试不同效果
//        System.out.println("=== 650ms, 120像素 ===");
//        String xml2 = generateRecoilXML(650, 120, 640, 360);
//        System.out.println(xml2);
    }

    /**
     * 鼠标点类
     */
    static class MousePoint {
        private int x;
        private int y;
        private int time;

        public MousePoint(int x, int y, int time) {
            this.x = x;
            this.y = y;
            this.time = time;
        }

        public int getX() { return x; }
        public int getY() { return y; }
        public int getTime() { return time; }
    }


    /**
     * 生成完整的XML宏文件
     */
    public static String generateFullMacroXML(String name, int totalTime, int pixelLength, int startX, int startY) {
        String pointsXML = RecoilMacroGenerator.generateRecoilXML(totalTime, pixelLength, startX, startY);

        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Macro>\n" +
                "   <Name>" + name + "</Name>\n" +
                "   <MacroEvents>\n" +
                "      <MacroEvent>\n" +
                "         <Type>3</Type>\n" +
                "         <Id/>\n" +
                "         <Number>" + (totalTime / 1000.0) + "</Number>\n" +
                "         <MouseEvent>\n" +
                pointsXML +
                "         </MouseEvent>\n" +
                "         <selected>false</selected>\n" +
                "      </MacroEvent>\n" +
                "   </MacroEvents>\n" +
                "   <DelaySetting>0</DelaySetting>\n" +
                "   <Guid>" + java.util.UUID.randomUUID() + "</Guid>\n" +
                "   <Version>4</Version>\n" +
                "   <MouseMoveType>relative</MouseMoveType>\n" +
                "</Macro>";
    }
}
